var $changeSure, $changeCancel, $promt;
var changePValidform;
// 绑定事件
function bindChangeEvent() {
    $changeSure.on("click", function() {
        if (changePValidform.check()) {
            var oldPassword = $("#old-password").val();
            var newPassword = $("#new-password2").val();
            if (newPassword.length > 7 && newPassword.search(/\d/) != -1 && newPassword.search(/[a-zA-Z]/) != -1) {
                $('#loding').modal('show');
                $.post(uppwdServlet, {
                    oldPwd: oldPassword,
                    newPwd: newPassword
                }, changeBack);
            } else {
                $("#prompt-modal").modal("show");
                $promt.text("新密码至少8位，须同时包含字母和数字");
            }

        }
    });
}

// 修改密码回调
function changeBack(bdata) {
    $('#loding').modal('hide');

    if (bdata.code == "login_need") {
        location.href = "./index.html";
    } else if (bdata.code == "0000") {
        $("#prompt-modal").modal("show");
        $promt.text("修改密码成功");
    } else {
        $("#prompt-modal").modal("show");
        $promt.text("修改密码失败");
    }

}

// 初始化变量
(function initLogOutVal() {
    $changeSure = $("#change-password_sure");
    $changeCancel = $("#change-password_cancel");
    $promt = $("#promt-body");
    changePValidform = $("#change-password_form").Validform({
        tiptype: 2
    });
})();

module.exports = {
    bindChangeEvent: bindChangeEvent
};